import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

//Test comment by Nisha

/**
 * Main class for creating an address book
 * 
 * @author Chia-Chi (Teresa) Lee
 */ 
public class AddressBook extends JSplitPane {
  
  private JTable table;
  private JTextArea display;
  //address table columns
  private String[] columnNames = {"Name","Phone Number","Address","Email"};
  //address table data
  private Object[][] data = {
    {"Kathy Smith","6781514512", "1234 Random Road, Atlanta, GA 30303", "ksmith@fake.com"},
    {"John Doe","6781514111", "1224 Random Road, Atlanta, GA 30303","johndoe@fake.com"},
    {"Sue Black","6789804111", "784 Green Road, Atlanta, GA 30301", "sblack@fake.com"},
    {"Jane White","6884804941", "704 Green Road, Atlanta, GA 30301", "jwhite@fake.com"},
    {"Joe Brown","6302180941", "1000 Tech Drive, Atlanta, GA 30301", "jbrown@fake.com"},
    {"Kathy Smith","6781514512", "1234 Random Road, Atlanta, GA 30303", "ksmith@fake.com"},
    {"John Doe","6781514111", "1224 Random Road, Atlanta, GA 30303","johndoe@fake.com"},
    {"Sue Black","6789804111", "784 Green Road, Atlanta, GA 30301", "sblack@fake.com"},
    {"Jane White","6884804941", "704 Green Road, Atlanta, GA 30301", "jwhite@fake.com"},
    {"Joe Brown","6302180941", "1000 Tech Drive, Atlanta, GA 30301", "jbrown@fake.com"}
};
  
  /**
   * Create initial interface of the Address Book tab
   * 
   */ 
  public void init(){
    //begin creating address list
    table = new JTable(data, columnNames);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.getSelectionModel().addListSelectionListener(new SelectionListener());
    JScrollPane listPane = new JScrollPane(table);
    
    //being creating botom display area
    display = new JTextArea();
    JScrollPane displayPane = new JScrollPane(display);
    
    setOrientation(JSplitPane.VERTICAL_SPLIT);
    setTopComponent(listPane);
    setBottomComponent(displayPane);
  }
  
  /**
   * Inner class for address book listener
   * 
   */ 
  class SelectionListener implements ListSelectionListener {
   
    public void valueChanged(ListSelectionEvent e) {
      
      //If user is done with deselecting and selecting, display info; otherwise, erase textarea content
      if(!e.getValueIsAdjusting())
      {
        int selection = table.getSelectedRow();
        for(int i=0;i<table.getColumnCount();i++){
          display.append(table.getColumnName(i)+": \n"+table.getValueAt(selection,i)+"\n\n");
        }
      }
      else
      {
        display.setText("");
      }
    }
  }
  
}